package com.liferay.controller;

import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.contant.Contants;
import com.liferay.model.ClassSubject;
import com.liferay.model.Faculty;
import com.liferay.model.Lesson;
import com.liferay.model.ListSubjectOpen;
import com.liferay.model.Room;
import com.liferay.model.Subject;
import com.liferay.model.SubjectOpen;
import com.liferay.model.Teacher;
import com.liferay.model.YearOfSemester;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.service.FacultyService;
import com.liferay.service.SubjectOpenService;
import com.liferay.service.YearOfSemesterService;
import com.liferay.service.impl.FacultyServiceImpl;
import com.liferay.service.impl.SubjectOpenServiceImpl;
import com.liferay.service.impl.YearOfSemesterImpl;

/**
 * Portlet implementation class SubjectOpenController
 */

@Controller(value = "SubjectOpenController")
@RequestMapping("VIEW")
public class SubjectOpenController {
	private List<YearOfSemester> listSemester;
	private List<Faculty> listFaculty;
	private List<Subject> listSubject;
	private List<ClassSubject> listClassSubject;
	private List<Room> listRoom;
	private List<Lesson> listLesson;
	private List<Teacher> listTeacher;
	private List<YearOfSemester> listYear;
	private List<ListSubjectOpen> listSubjectOpen;
	private List<SubjectOpen> listSbOpen;
	private YearOfSemesterService yearOfSemesterService;
	private FacultyService facultyService;
	private SubjectOpenService subjectOpenService;
	private int semes = 0;
	private long idFaculty;
	private long idYear = 0;
	private long idYearR = 0;
	private long idYearS = 0;
	private long idDecrease = 0;
	private long idSubjectOpen = 0;
	private String yearOfSemes = "";
	private static Logger _log = Logger.getLogger(SubjectOpenController.class);

	@RenderMapping
	public String showPageView(RenderRequest renderRequest,
			RenderResponse renderResponse, Model model) {
		// semester list
		_log.info("subject...");
		yearOfSemesterService = new YearOfSemesterImpl();
		listSemester = yearOfSemesterService.getMaxSemester();
		_log.info(listSemester);
		model.addAttribute("listSemester", listSemester);

		// faculty
		_log.info("faculty...");
		facultyService = new FacultyServiceImpl();
		listFaculty = facultyService.getAllFaculty();
		_log.info(listFaculty);
		model.addAttribute("listFaculty", listFaculty);

		// get subject by faculty
		_log.info("show Sub by Fac");
		facultyService = new FacultyServiceImpl();
		listSubject = facultyService.getSubjectByFaculty(idFaculty);
		model.addAttribute("listSubject", listSubject);
		_log.info(listSubject);
		model.addAttribute("idFaculty", idFaculty);

		_log.info("Show list subject open by semester = " + idYearR);
		subjectOpenService = new SubjectOpenServiceImpl();
		listSubjectOpen = subjectOpenService
				.getListSubjectOpenByYearOfSemester(idFaculty, idYearR);
		model.addAttribute("listSubjectOpen", listSubjectOpen);
		_log.info(listClassSubject);
		_log.info("-----------------------------------------------");
		return "view";
	}

	@ActionMapping(params = "action=increaseYearOfSemester")
	public void increaseYearOfSemester(ActionRequest actionRequest,
			ActionResponse actionResponse, Model model) {
		try {
			_log.info("Increase year of semester...");
			idYear = ParamUtil.getLong(actionRequest, "idYear");
			semes = ParamUtil.getInteger(actionRequest, "semes");
			_log.info("Id year " + idYear);
			yearOfSemes = ParamUtil.getString(actionRequest, "yearOfSemes");
			yearOfSemesterService = new YearOfSemesterImpl();

			if (semes == 2) {
				semes = semes - 1;
				String token = yearOfSemesterService
						.checkStringToken(yearOfSemes);
				YearOfSemester year = new YearOfSemester();
				year.setId(idYear);
				year.setSemester(semes);
				year.setYearOfSemester(token);
				yearOfSemesterService.insertSemester(year);
				_log.info(semes + " " + yearOfSemes);
				_log.info("SUCCESS");
				idYearR = ParamUtil.getLong(actionRequest, "idYear");
				SessionMessages.add(actionRequest.getPortletSession(),
						"success");

			} else if (semes == 1) {
				semes += 1;
				_log.info(semes);
				YearOfSemester year2 = new YearOfSemester();
				year2.setId(idYear);
				year2.setSemester(semes);
				year2.setYearOfSemester(yearOfSemes);
				yearOfSemesterService.insertSemester(year2);
				_log.info(semes + " " + yearOfSemes);
				_log.info("SUCCESS");
				idYearR = ParamUtil.getLong(actionRequest, "idYear");
				SessionMessages.add(actionRequest.getPortletSession(),
						"success");
				_log.info("-----------------------------------------------");
			}
		} catch (Exception e) {
			e.printStackTrace();
			SessionErrors.add(actionRequest.getPortletSession(), "fail");
		}
	}

	// @ActionMapping(params = "action=decreaseYearOfSemester")
	// public void decreaseYearOfSemester(ActionRequest
	// actionRequest,ActionResponse actionResponse){
	// actionResponse.setRenderParameter("action", "demo");
	// }
	//
	// @RenderMapping(params = "action=demo")
	// public String demo(RenderRequest renderRequest, RenderResponse
	// renderResponse, Model model){
	// return "semester";
	// }

	// get list faculty
	@ActionMapping(params = "action=showListFaculty")
	public void showListFaculty(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		_log.info("-----------------------------------------------");
	}

	@ActionMapping(params = "action=showSubjectByFaculty")
	public void showSubjectByFaculty(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		idFaculty = ParamUtil.getLong(actionRequest, "idFaculty");
		idYearR = ParamUtil.getLong(actionRequest, "idYearR");
		_log.info("Subject by faculty = " + idFaculty);
		_log.info("-----------------------------------------------");
	}

	@ActionMapping(params = "action=showPageInsertNewSubject")
	public void showPageInsertNewSubject(ActionRequest actionRequest,
			ActionResponse actionResponse, Model model) {
		_log.info("Faculty Page NewSubject " + idFaculty);
		idYearS = ParamUtil.getLong(actionRequest, "idYearS");

		_log.info("idFaculty page insert new subject = " + idFaculty);

		actionResponse.setRenderParameter("action", "handleShowPage");
		_log.info("-----------------------------------------------");
	}

	@RenderMapping(params = "action=handleShowPage")
	public String handleShowPage(RenderRequest renderRequest,
			RenderResponse renderResponse, Model model) {
		// model.addAttribute("idFaculty", idFaculty);
		// yearOfSemesS = ParamUtil.getString(renderRequest, "yearOfSemesS");
		// semesS = ParamUtil.getInteger(renderRequest, "semesS");
		// Subject
		_log.info("List Subject By Faculty =" + idFaculty);
		listSubject = facultyService.getSubjectByFaculty(idFaculty);
		model.addAttribute("listSubject", listSubject);
		_log.info(listSubject);

		// Class
		_log.info("List Class by Faculty = " + idFaculty);
		subjectOpenService = new SubjectOpenServiceImpl();
		listClassSubject = subjectOpenService.getClassByFaculty(idFaculty);
		model.addAttribute("listClassSubject", listClassSubject);
		_log.info(listClassSubject);

		// Room
		_log.info("List Room");
		listRoom = subjectOpenService.getListRoom();
		model.addAttribute("listRoom", listRoom);
		_log.info(listRoom);

		// Lesson
		_log.info("List Lesson");
		listLesson = subjectOpenService.getListLesson();
		model.addAttribute("listLesson", listLesson);
		_log.info(listLesson);

		// Teacher
		_log.info("List Teacher By Faculty =" + idFaculty);

		listTeacher = subjectOpenService.getTeacherByFaculty(idFaculty,
				Contants.ROLE_TEACHER);
		model.addAttribute("listTeacher", listTeacher);
		_log.info(listTeacher);

		// idYear
		_log.info("Id Year of Semester");
		listYear = yearOfSemesterService.getListYearOfSemester(idYearS);
		model.addAttribute("listYear", listYear);
		_log.info(listYear);
		// model.addAttribute("idYearS",idYearS);
		// model.addAttribute("yearOfSemesS",yearOfSemesS);
		// model.addAttribute("semesS",semesS);

		// _log.info(yearOfSemes);
		// _log.info(semes);
		_log.info("-----------------------------------------------");
		return "newsubjectopen";
	}

	@ActionMapping(params = "action=insertSubjectOpenByFaculty")
	public void insertNewSubjectByFacultyURL(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			_log.info("insert Subject Open");

			long subjectOpen = ParamUtil.getLong(actionRequest, "subjectOpen");
			long teacherOpen = ParamUtil.getLong(actionRequest, "teacherOpen");
			int roomOpen = ParamUtil.getInteger(actionRequest, "roomOpen");
			long classOpen = ParamUtil.getLong(actionRequest, "classOpen");
			long lessonOpen = ParamUtil.getLong(actionRequest, "lessonOpen");
			long yearOpen = ParamUtil.getLong(actionRequest, "yearOpen");
			// String dateStart = ParamUtil.getString(actionRequest,
			// "date-start");
			// String dateEnd = ParamUtil.getString(actionRequest, "date-end");

			// date-picker
			_log.info("DayStart: " + ParamUtil.getString(actionRequest, "d1")
					+ "-" + ParamUtil.getString(actionRequest, "m1") + "-"
					+ ParamUtil.getString(actionRequest, "y1"));
			_log.info("DayEnd: " + ParamUtil.getString(actionRequest, "d2")
					+ "-" + ParamUtil.getString(actionRequest, "m2") + "-"
					+ ParamUtil.getString(actionRequest, "y2"));

			String dateStart = ParamUtil.getString(actionRequest, "y1") + "-"
					+ ParamUtil.getString(actionRequest, "m1") + "-"
					+ ParamUtil.getString(actionRequest, "d1");
			String dateEnd = ParamUtil.getString(actionRequest, "y2") + "-"
					+ ParamUtil.getString(actionRequest, "m2") + "-"
					+ ParamUtil.getString(actionRequest, "d2");

			// SimpleDateFormat formatter1 = new SimpleDateFormat("dd/MM/yyyy");
			// Date dateCase = (Date) formatter1.parse(date);

			_log.info("INSERT....");
			SubjectOpen subOpen = new SubjectOpen();
			subOpen.setFkSubject(subjectOpen);
			subOpen.setFkTeacher(teacherOpen);
			subOpen.setFkRoom(roomOpen);
			subOpen.setFkClass(classOpen);
			subOpen.setFkYearOfSemester(yearOpen);
			subOpen.setFkLesson(lessonOpen);
			subOpen.setDate_start(dateStart);
			subOpen.setDate_end(dateEnd);
			subjectOpenService.insertSubjectOpenByFaculty(subOpen);

			_log.info("SUCCESS");
			SessionMessages.add(actionRequest.getPortletSession(), "success");
			_log.info("-----------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
			SessionErrors.add(actionRequest.getPortletSession(), "fail");
		}
	}

	@ActionMapping(params = "action=insertSubjectByFaculty")
	public void insertSubjectByFaculty(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			String nameSubject = ParamUtil.getString(actionRequest,
					"nameSubject");
			int numberOfCredit = ParamUtil.getInteger(actionRequest,
					"numberOfCredit");
			long idFaculty = ParamUtil.getLong(actionRequest, "idFaculty");
			_log.info("Insert Subject By Faculty =" + idFaculty);
			Subject subject = new Subject();
			subject.setNameSubject(nameSubject);
			subject.setNumberOfCredit(numberOfCredit);
			subject.setFkFaculty(idFaculty);
			facultyService = new FacultyServiceImpl();
			facultyService.insertSubjectByFaculty(subject);
			_log.info("SUCCESS");
			SessionMessages.add(actionRequest.getPortletSession(), "success");
			_log.info("-----------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
			SessionErrors.add(actionRequest.getPortletSession(), "fail");
		}
	}

	@RenderMapping(params = "action=showPageInsertNewSubjectByFaculty")
	public String showPageInsertNewSubjectByFaculty(
			RenderRequest renderRequest, RenderResponse renderResponse,
			Model model) {
		facultyService = new FacultyServiceImpl();
		listFaculty = facultyService.getAllFaculty();
		model.addAttribute("listFaculty", listFaculty);
		model.addAttribute("idFaculty", idFaculty);
		_log.info(listFaculty);
		_log.info("-----------------------------------------------");
		return "newsubject";
	}

	@ActionMapping(params = "action=decreaseSemesterById")
	public void decreaseSemesterById(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			_log.info("Decrease...");
			idDecrease = ParamUtil.getLong(actionRequest, "idDecrease");
			// decrease semester
			yearOfSemesterService = new YearOfSemesterImpl();
			yearOfSemesterService.deleteSemester(idDecrease);
			idYearR = ParamUtil.getLong(actionRequest, "idYearR");
			_log.info("SUCCESS");
			SessionMessages.add(actionRequest.getPortletSession(), "success");
			_log.info("-----------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
			SessionErrors.add(actionRequest.getPortletSession(), "fail");
		}
	}

	@ActionMapping(params = "action=deleteCheckedSubjectById")
	public void deleteCheckedSubjectByFaculty(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			String[] itemId = ParamUtil.getParameterValues(actionRequest,
					"selected");
			_log.info("Delete checked Subject By Faculty with id " + itemId);
			facultyService = new FacultyServiceImpl();
			if (itemId != null) {
				for (int i = 0; i < itemId.length; i++) {
					_log.info("item: " + itemId[i]);
					long items = Long.parseLong(itemId[i]);
					facultyService.deleteSubjectById(items);
				}
			}
			_log.info("SUCCESS");
			SessionMessages.add(actionRequest.getPortletSession(), "success");
			actionResponse.setRenderParameter("action", "showPageView");
			_log.info("-----------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
			SessionErrors.add(actionRequest.getPortletSession(), "error");
		}
	}

	@ActionMapping(params = "action=deleteCheckedSubjectOpenById")
	public void deleteCheckedSubjectOpenById(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			_log.info("Delete Checked Subject Open By Id");
			idSubjectOpen = ParamUtil.getLong(actionRequest,
					"idSubjectOpen");
			subjectOpenService = new SubjectOpenServiceImpl();
			// if (itemId != null) {
			// for (int i = 0; i < itemId.length; i++) {
			// long items = Long.parseLong(itemId[i]);
			_log.info("Deleting..." + idSubjectOpen);
			subjectOpenService.deleteSubjectOpenById(idSubjectOpen);
			// }
			// }
			_log.info("SUCCESS");
			_log.info("-----------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
			SessionErrors.add(actionRequest.getPortletSession(), "error");
		}
	}

	@ActionMapping(params = "action=showPageUpdateSubjectOpenById")
	public void showPageUpdateSubjectOpenById(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		idSubjectOpen = ParamUtil.getLong(actionRequest, "idSubjectOpen");
		_log.info("Id subject " + idSubjectOpen);
		actionResponse
				.setRenderParameter("action", "handleEditPageSubjectOpen");
	}

	@RenderMapping(params = "action=handleEditPageSubjectOpen")
	public String handleEditPageSubjectOpen(RenderRequest renderRequest,
			RenderResponse renderResponse, Model model) {
		subjectOpenService = new SubjectOpenServiceImpl();
		listSbOpen = subjectOpenService
				.getListSubjectOpenById(idSubjectOpen);
		return "updateSubjectOpen";
	}
}
